package com.github.sbb.sherry.primitives.config.datasource;

import com.github.sbb.sherry.common.base.Preconditions;
import com.github.sbb.sherry.common.base.ValidateResult;
import com.github.sbb.sherry.common.primitives.StringUtil;

/**
 * Redis 数据源配置
 */
public class RedisDataSourceConfig implements DataSourceConfig {

  private String host;
  private int port = 6379;
  /**
   * 超时时间，毫秒
   */
  private int timeout = 2500;

  private String password;

  private int database = 0;

  // 最小空闲连接数
  private int minIdle = 0;
  // 最大空闲连接数
  private int maxIdle = 8;
  // 最大连接数
  private int maxActive = 8;

  // 最大等待时间
  private int maxWait;

  @Override
  public ValidateResult validate() {
    try {
      Preconditions.checkNotNull(host, "host 不能为空");
      Preconditions.checkRange(port, 1, 65535, "port 应该在 1 - 65535 之间");
      Preconditions.checkRange(timeout, 10, Integer.MAX_VALUE, StringUtil.lenientFormat("timeout 应该在 10 - {} 之间", Integer.MAX_VALUE));
      Preconditions.checkNotNull(password, "password 不能为空（虽然是可以空的，但这里不建议）");
      Preconditions.checkRange(database, 0, 65535, "database 应该在 1 - 65535 之间");
      Preconditions.checkRange(minIdle, 0, 65535, "minIdle 应该在 1 - 65535 之间");
      Preconditions.checkRange(maxIdle, minIdle, 65535, StringUtil.lenientFormat("maxIdle 应该在 {} - 65535 之间", minIdle));
      Preconditions.checkRange(maxActive, maxIdle, 65535, StringUtil.lenientFormat("maxActive 应该在 {} - 65535 之间", maxIdle));
      Preconditions.checkRange(maxWait, 0, Integer.MAX_VALUE, StringUtil.lenientFormat("maxWait 应该在 10 - {} 之间", Integer.MAX_VALUE));
    } catch (Exception e) {
      return new ValidateResult(false, e.getMessage());
    }
    return new ValidateResult(true, null);
  }

  public String getHost() {
    return host;
  }

  public void setHost(String host) {
    this.host = host;
  }

  public int getPort() {
    return port;
  }

  public void setPort(int port) {
    this.port = port;
  }

  public int getTimeout() {
    return timeout;
  }

  public void setTimeout(int timeout) {
    this.timeout = timeout;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public int getDatabase() {
    return database;
  }

  public void setDatabase(int database) {
    this.database = database;
  }

  public int getMinIdle() {
    return minIdle;
  }

  public void setMinIdle(int minIdle) {
    this.minIdle = minIdle;
  }

  public int getMaxIdle() {
    return maxIdle;
  }

  public void setMaxIdle(int maxIdle) {
    this.maxIdle = maxIdle;
  }

  public int getMaxActive() {
    return maxActive;
  }

  public void setMaxActive(int maxActive) {
    this.maxActive = maxActive;
  }

  public int getMaxWait() {
    return maxWait;
  }

  public void setMaxWait(int maxWait) {
    this.maxWait = maxWait;
  }
}
